#include "Weasel.h"
#include "ResourceManagers.h"
#include "ChickenDefGame.h"
#include <math.h>

Weasel::Weasel(ChickenDefGame * _game)
{
	game=_game;
	//cargamos las animaciones
	m_aniWalk= new AniSprite(*ImageManager::Instance()->GetImage("comadreja.png"),96,96);
	m_aniCurrent= m_aniWalk;
	m_aniCurrent->SetLoopSpeed(5);
	m_aniCurrent->Play(0,2);
	alive=false;
	speed=0.2f;
	hitSound.SetBuffer(*SoundBufferManager::Instance()->GetBuffer("comadreja.wav"));
	

	
	}

bool Weasel::IsAlive(){
	return alive;
}
Weasel::~Weasel(void)
{

}

Vector2f Weasel::GetCenter(){

	Vector2f center= m_aniCurrent->GetCenterPosition();
	return center;

}

float Weasel::GetCircleRadius(){

	return m_aniCurrent->GetFrameCircleRadius()-40;
}

void Weasel::Update(float dt){

	//movemos en dir
	Vector2f pos= m_aniCurrent->GetPosition();
	pos.x+=dir.x * speed;
	pos.y+=dir.y *speed;

	//si llegamos al medio deberiamos perder
	//por ahora solo morimos
	if( sqrtf((400-pos.x)*(400-pos.x)+(300-pos.y)*(300-pos.y)) <10){
		game->GameOver();
		//Die();
	}

	m_aniCurrent->SetPosition(pos);



	m_aniCurrent->Update();


		
}


void Weasel::Draw(RenderWindow* wnd){
	
	

	//draw collision circle for debug only
	/*Shape shp=Shape::Circle(m_aniCurrent->GetCenterPosition(),m_aniCurrent->GetFrameCircleRadius()-40,Color::Black);
	wnd->Draw(shp);*/

	wnd->Draw(*m_aniCurrent);


}

void Weasel::Move(int dx, int dy){


}
	

void Weasel::Reset(){

	Vector2f pos;
	//elegimos un lugar aleatorio para entrar
	int wherefrom=rand()%4;

	switch(wherefrom){
	
	case 0: //entra por la izquierda
		pos.x=-10;
		pos.y=(float)( rand()%600+1);
	break;
	case 1: //entra por arriba
		pos.y=-10;
		pos.x=(float)( rand()%800+1);
		break;
	case 2: //entra por abajo
		pos.y=610;
		pos.x=(float)(rand()%800+1);
		break;
	case 3:
		pos.x=810;
		pos.y=(float)( rand()%600+1);

	}
	

	//seteamos animacion de caminar
	m_aniCurrent= m_aniWalk;
	m_aniCurrent->Play(0,2);
	m_aniCurrent->SetPosition(pos);

	//la orientamos al centro
	dir.x = 400 - pos.x ;
	dir.y = 300 - pos.y;
	float norm= sqrtf(dir.x*dir.x+dir.y*dir.y);
	dir.x/=norm;
	dir.y/=norm;

	float alfa= 180.0f/3.1516f * acosf(-dir.y);
	if(dir.x>0)
		alfa*=-1;
	
	m_aniCurrent->SetRotation(alfa);

	//esta vivaaaaa mua ja ja
	alive=true;

}

void Weasel::Die(){

	hitSound.Play();
	
//Ocultamos el sprite
	m_aniCurrent->Stop();
	alive=false;
	
}

void Weasel::SilentDeath(){
	m_aniCurrent->Stop();
	alive=false;
}